window.onload = getMyLocation;


function getMyLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayLocation, displayError);
        // alert("Browser do support, but don't know why it doesn't show up");
        var button = document.getElementById("update_location")
        button.onclick = updateLocation;
    } else {
        alert("Oops, no geolocation support");
    }
}

function displayLocation(position) {
    alert("work")
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    var div = document.getElementById("location")
    div.innerHTML = "your are at Latitude: " + latitude + ", Longitude: " + longitude;
    div.innerHTML += " (with " + position.coords.accuracy + " meters accuracy";
}

function displayError(error) {
    alert("not work")
    var errorTypes = {
        0: "Unknown error",
        1: "Permission denied by user",
        2: "Position is not available",
        3: "Request timed out"
    };
    var errorMessage = errorTypes[error.code];
    if (error.code == 0 || error.code == 2) {
        errorMessage = errorMessage + " " + error.message;
    }
    var div = document.getElementById("location");
    div.innerHTML = errorMessage;
}

function updateLocation() {
    // alert("This is a test")
    navigator.geolocation.getCurrentPosition(displayLocation, displayError);
    var div = document.getElementById("location");
    div.innerHTML = "update";
}